import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

public class Corpus {
	private Vector corpus_;
	
	public Corpus(Vector corpus){
		corpus_ = corpus;
	}
	
	public static Corpus createCorpus(Options options) throws IOException{
		
		Vector corpus_files = new Vector();
		CorpusFile curr_corpus_file;
		File[] input_files = options.getInputFile();
		InputStream file_input = null;
		char curr_char;
		String curr_word = "";
		boolean isThereStopFile;
		int curr_line_num = 0;
		boolean newline_check = false;
		boolean newline = true;
		
		if(options.getWordAnalyzer() == null)
			isThereStopFile = false;
		else
			isThereStopFile = true;
		
		for(int i = 0; i < input_files.length; i++){
			if(!input_files[i].exists()){
				throw new FileNotFoundException("unable to process input file");
			}
			file_input = new FileInputStream(input_files[i]);
			if(file_input == null){
				throw new FileNotFoundException("unable to process input file");
			}
			
			curr_corpus_file = new CorpusFile(input_files[i].getName());
				
			while(true){
				try{
					curr_char = (char)file_input.read();
					curr_corpus_file.addChar();
				}
				catch(IOException ioe)
				{
					throw new IOException("unable to read text file");
				}
				
				//if curr_char is EOF or a break character...
				if(curr_char == 65535 || CharacterAnalyzer.isBreakCharacter(curr_char)) {
					

					
					if(newline){
						curr_line_num++;
						curr_corpus_file.addLine();
						newline = false;
					}
					
					//if we have a new line, increment the curr_line_num count
					if(curr_char == 10 || curr_char == 13){
						if(curr_char == 13){
							newline_check = true;
							newline = true;
							
						}
						else{
							if(newline_check){
								newline_check = false;
								curr_corpus_file.subtractChar();
							}
							else{
								newline = true;
							}
						}
						
					}
					
					//if curr_word isn't an empty string, meaning we've found a new word...
					if(curr_word.length() != 0){
						//if we need to compare the new word to the stop words...
						if(isThereStopFile && options.getWordAnalyzer().isStopWord(curr_word.toLowerCase())){
							curr_word = "";
						}
						else{
							curr_word = curr_word.toLowerCase();
							if(curr_corpus_file.contains(curr_word)){
								curr_corpus_file.getCorpusWord(curr_word).addLineNumber(curr_line_num);
							}
							else{
								curr_corpus_file.addCorpusWord(new CorpusWord(curr_word, curr_line_num));
								curr_corpus_file.addWord();
							}
							curr_word = "";
						}
					}	
					if(curr_char == 65535){
						curr_line_num = 0;
						newline = true;
						curr_corpus_file.subtractChar();
						file_input.close();
						break;
					}
				}
				else{
					curr_word = curr_word.concat((new Character(curr_char)).toString());	
				}
			}
			corpus_files.add(curr_corpus_file);
		}
		
		return new Corpus(corpus_files);
	}
	
	public Vector getCorpusFiles(){		
		return corpus_;
	}
}
